package com.coke.elastic.search.controller;


import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.coke.elastic.search.entity.User;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
public class TestController {
    @Autowired
    private RestHighLevelClient restHighLevelClient;

    private static final String INDEX ="user";
private final static List<User> map =new ArrayList(){
    {
        add(new User(1,"张三",new Date()));
        add(new User(2,"李四",new Date()));
        add(new User(3,"王五",new Date()));
        add(new User(4,"赵六",new Date()));
        add(new User(5,"陈八",new Date()));
    }
};
    @PutMapping("/add")
    public void add() throws IOException {
        for (User user:map
             ) {
            IndexRequest indexRequest = new IndexRequest(INDEX);
            indexRequest.id(user.getId().toString());
            JSON parse = JSONUtil.parse(user);
            indexRequest.source(parse, XContentType.JSON);
            restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
        }


    }
}
